package com.ficus.road.maintain.third.zh.smart;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.Coordinate;
import com.ficus.road.maintain.core.model.RoadTypeEnum;
import com.ficus.road.maintain.core.model.smartnew.PrimaryRoadInfoZH;
import com.ficus.road.maintain.core.model.smartnew.PrimaryRoadSectionDTO;
import com.ficus.road.maintain.dao.smartnew.primaryScreening.PrimaryRoadSectionDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.ficus.road.maintain.util.LngLonUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Set;

/**
 调用中海接口获取智慧决策道路基础数据
 */
@Component
@Slf4j
public class PrimaryRoadGpsInfoZH extends BaseZH {
    @Autowired
    PrimaryRoadSectionDao primaryRoadSectionDao;


    void init() {
        arg0 = "ytkjRoadLatticeData";
        arg1 = "{}";
        errMsg = "调用中海接口获取智慧决策道路基础数据异常!";
        successMsg = "调用中海接口获取智慧决策道路基础数据成功!";
    }

//    @PostConstruct
    public void refreshBasicRoadInfo() {
        init();
        String basicRoadStr = getZHData();
        if (ObjectUtils.isEmpty(basicRoadStr)) {
            return;
        }
        List<PrimaryRoadInfoZH> basicRoadZHList = JSONArray.parseArray(basicRoadStr, PrimaryRoadInfoZH.class);

        List<PrimaryRoadSectionDTO> lists = Lists.newArrayList();
        Set<String> ids = Sets.newHashSet();
        basicRoadZHList.forEach(zh -> {
            PrimaryRoadSectionDTO roadId = primaryRoadSectionDao.findOne(new Query(Criteria.where("road_id").is(zh.getUnid())));
            if (roadId == null){
                PrimaryRoadSectionDTO roadSectionDTO = new PrimaryRoadSectionDTO();
                roadSectionDTO.setRoadId(zh.getUnid());
                roadSectionDTO.setRoadCode(zh.getRoadcode());
                roadSectionDTO.setRoadName(zh.getRoadname());
                roadSectionDTO.setStartStake(StringUtils.isEmpty(zh.getStartlocationdesc()) ? null : Double.parseDouble(zh.getStartlocationdesc()));
                roadSectionDTO.setEndStake(StringUtils.isEmpty(zh.getEndlocationdesc()) ? null : Double.parseDouble(zh.getEndlocationdesc()));
                roadSectionDTO.setWidth(StringUtils.isEmpty(zh.getRoadwidth()) ? null : Double.parseDouble(zh.getRoadwidth()));
                roadSectionDTO.setLength(StringUtils.isEmpty(zh.getRoadlength()) ? roadSectionDTO.getEndStake() - roadSectionDTO.getStartStake() : Double.parseDouble(zh.getRoadlength()));
                roadSectionDTO.setArea(StringUtils.isEmpty(zh.getRoadarea()) ? null : Double.parseDouble(zh.getRoadarea()));

                roadSectionDTO.generalRoadId();
                if (ids.contains(roadSectionDTO.getId())) {
                    return;
                } else {
                    ids.add(roadSectionDTO.getId());
                }

                if (!StringUtils.isEmpty(zh.getGdzb())) {
                    List<Coordinate> coordinates = JSONArray.parseArray(zh.getGdzb(), Coordinate.class);
                    coordinates.forEach(coordinate -> {
                        double[] wgs84 = LngLonUtil.gcj02towgs84(Double.parseDouble(coordinate.getX()), Double.parseDouble(coordinate.getY()));
                        coordinate.setX(String.valueOf(wgs84[0]));
                        coordinate.setY(String.valueOf(wgs84[1]));
                    });
                    roadSectionDTO.setCoordinates(coordinates);
                }
                roadSectionDTO.setGdzb(zh.getGdzb());
                roadSectionDTO.setLevel(zh.getRoadgrade());
                roadSectionDTO.setRoadType(zh.getRoadtype());
                roadSectionDTO.setSource(zh.getSources());
                roadSectionDTO.setLastOverhaulYear(StringUtils.isEmpty(zh.getMaintenacetime()) ? null : Double.parseDouble(zh.getMaintenacetime()));
                lists.add(roadSectionDTO);
            }else {
                primaryRoadSectionDao.update(roadId.getId(), Update.update("gdzb", zh.getGdzb()));
            }

        });

//        primaryRoadSectionDao.deleteAll();
        if (!CollectionUtils.isEmpty(lists)){
            primaryRoadSectionDao.insert(lists);
        }

    }

    public RoadTypeEnum getRoadType(String roadType) {
        RoadTypeEnum res = null;
        switch (roadType) {
            case "城市道路":
                res = RoadTypeEnum.HIGHWAY;
                break;
            case "直管公路":
                res = RoadTypeEnum.DIRECT;
                break;
            case "农村公路":
                res = RoadTypeEnum.ROAD;
                break;
            default:
                break;
        }
        return res;
    }
}
